package hu.monitor.remoting;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class InvocationProxyFactory {
	private final Class<?> beanInterface;
	private final InterceptorChain interceptorChain;

	public InvocationProxyFactory(final Class<?> beanInterface, final InterceptorChain interceptorChain) {
		this.interceptorChain = interceptorChain;
		this.beanInterface = beanInterface;
	}

	public Class<?> getBeanInterface() {
		return this.beanInterface;
	}

	@SuppressWarnings("unchecked")
	public <T> T newProxy() {
		final ClassLoader classLoader = this.getClass().getClassLoader();
		final Class<?>[] interfaces = new Class[]{this.beanInterface};
		final InvocationHandler handler = new InterceptorInvocationHandler(this.interceptorChain, this.beanInterface);
		return (T)Proxy.newProxyInstance(classLoader, interfaces, handler);
	}
}
